package com.itheima.tanhua.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.tanhua.api.db.TestSoulApi;
import com.itheima.tanhua.mapper.TbSoulquestionMapper;
import com.itheima.tanhua.mapper.TbSoulquestionoptionMapper;
import com.itheima.tanhua.mapper.TbTestSoulMapper;
import com.itheima.tanhua.pojo.db.Option;
import com.itheima.tanhua.pojo.db.Question;
import com.itheima.tanhua.pojo.db.SoulQuestion;
import com.itheima.tanhua.pojo.soul.TbSoulquestion;
import com.itheima.tanhua.pojo.soul.TbSoulquestionoption;
import com.itheima.tanhua.pojo.soul.TbTestsoul;
import com.itheima.tanhua.vo.mongo.TestSoulVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class TestSoulApiImpl implements TestSoulApi {
    @Autowired
    TbTestSoulMapper tbTestSoulMapper;
    @Autowired
    TbSoulquestionMapper tbSoulquestionMapper;
    @Autowired
    TbSoulquestionoptionMapper tbSoulquestionoptionMapper;

    @Override
    public List<TestSoulVo> getAll() {
        List<TestSoulVo>list=new ArrayList<>();
        //1.先得到所有的灵魂测试卷面
        List<TbTestsoul> tbTestsoul=tbTestSoulMapper.selectList(new LambdaQueryWrapper<>());
        for (TbTestsoul testsoul : tbTestsoul) {
            //2.得到卷面的所有问题List questions;//测试体(非必须)
            List<SoulQuestion>questionList=new ArrayList<>();
            Long questions = testsoul.getQuestions();
            LambdaQueryWrapper<TbSoulquestion> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(TbSoulquestion::getTestId,questions);
            List<TbSoulquestion> tbSoulquestions = tbSoulquestionMapper.selectList(wrapper);//试卷的所有题目
            if (tbSoulquestions.size()==0|| ObjectUtil.isEmpty(tbSoulquestions)){//每一个问卷都有对应的题目和答案

            }else {//每一个问卷
                for (TbSoulquestion soulquestion : tbSoulquestions) {
                    //文卷下的所有问题集合

                    //问题的答案选项
                    Integer questionId = soulquestion.getQuestionid();
                    LambdaQueryWrapper<TbSoulquestionoption> soulquestionoptionLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    soulquestionoptionLambdaQueryWrapper.eq(TbSoulquestionoption::getSoulquestion, questionId);
                    List<TbSoulquestionoption> tbSoulquestionoptions = tbSoulquestionoptionMapper.selectList(soulquestionoptionLambdaQueryWrapper);//每一个选项对所有答案
                    List<Option> optionList = new ArrayList<>();
                    //将答案封装起来，添加对象
                    for (TbSoulquestionoption tbSoulquestionoption : tbSoulquestionoptions) {
                        Integer answerId = tbSoulquestionoption.getAnswerid();
                        String option = "";
                        if (tbSoulquestionoption.getOptionA() != null) {
                            option = tbSoulquestionoption.getOptionA();
                        }
                        if (tbSoulquestionoption.getOptionB() != null) {
                            option = tbSoulquestionoption.getOptionB();
                        }
                        if (tbSoulquestionoption.getOptionC() != null) {
                            option = tbSoulquestionoption.getOptionC();
                        }
                        if (tbSoulquestionoption.getOptionD() != null) {
                            option = tbSoulquestionoption.getOptionD();
                        }
                        if (tbSoulquestionoption.getOptionE() != null) {
                            option = tbSoulquestionoption.getOptionE();
                        }
                        if (tbSoulquestionoption.getOptionF() != null) {
                            option = tbSoulquestionoption.getOptionF();
                        }
                        if (tbSoulquestionoption.getOptionG() != null) {
                            option = tbSoulquestionoption.getOptionG();
                        }
                        Option opt = new Option(answerId + "", option);
                        optionList.add(opt);
                    }
                    SoulQuestion soulQuestion = new SoulQuestion();
                    soulQuestion.setId(soulquestion.getQuestionid() + "");
                    soulQuestion.setQuestion(soulquestion.getQuestion());
                    soulQuestion.setOptions(optionList);
                    questionList.add(soulQuestion);
                }
            }
            TestSoulVo soulVo=TestSoulVo.tobeTestVo(testsoul);
            soulVo.setQuestions(questionList);
            list.add(soulVo);
        }
        return list;
    }
//分数集合
    @Override
    public Integer getTotalScore(List<String> optionId) {
        LambdaQueryWrapper<TbSoulquestionoption>wrapper=new LambdaQueryWrapper<>();
        wrapper.in(TbSoulquestionoption::getAnswerid,optionId);
        List<TbSoulquestionoption> list = tbSoulquestionoptionMapper.selectList(wrapper);
        Integer total=0;
        for (TbSoulquestionoption soulquestionoption : list) {
            total+=soulquestionoption.getScore();
        }
        return total;
    }
}
